package com.wushijia.io;

import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * description:从文件中读取数据
 *
 * @author yang
 * @date 2018/1/7 19:57
 */
public class FileChannelTest {

  public static void main(String[] args) throws Exception {
    RandomAccessFile aFile = new RandomAccessFile("resources/data/nio-data.txt", "rw");
    FileChannel fileChannel = aFile.getChannel();
    ByteBuffer buffer = ByteBuffer.allocate(48);
    int bytesRead = fileChannel.read(buffer);
    while (bytesRead != -1) {
      System.out.println("Read:" + bytesRead);
      buffer.flip();

      while (buffer.hasRemaining()) {
        System.out.println((char) buffer.get());
      }
      buffer.clear();
      bytesRead = fileChannel.read(buffer);
    }
    aFile.close();
  }

}
